// pages/memberList/memberList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    corpsId: '',
    name: '',
    post: '',
    memberList: [],
    applyNum: 0,
    tuanzhangSrc: '../../image/member/tuanzhnag@2x.png',
    futuanzhangSrc: '../../image/member/futuanzhang@2x.png',
    tuanyuanSrc: '../../image/member/tuanyuan@2x.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      corpsId: options.corpsId,
      name: options.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      url: app.globalData.prefix + "/corpsAction/member.do",
      data: {
        openId: app.globalData.userInfo.openId,
        corpsId: this.data.corpsId
      },
      success: (res) => {
        let status = res.data.status
        if (status === "00") {
          let data = res.data.result
          let memberList = data.memberList
          let post = '03'
          memberList.forEach(member => {
            let self = member.self
            if(self){
              post = member.post
            }
          })
          this.setData({
            post: post,
            memberList: memberList,
            applyNum: data.applyNum
          })
        } else {
          wx.$showToast(res.data.errorMsg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  updatePost(e) {
    let memberId = e.currentTarget.dataset['member']
    wx.request({
      url: app.globalData.prefix + "/corpsAction/updatePost.do",
      data: {
        openId: app.globalData.userInfo.openId,
        memberId: memberId
      },
      method: 'POST',
      success: (res) => {
        let status = res.data.status
        if (status === "00") {
          wx.$showToast(res.data.result)
          this.onShow()
        } else {
          wx.$showToast(res.data.errorMsg)
        }
      }
    })
  },
  kickOut(e) {
    let memberId = e.currentTarget.dataset['member']
    wx.request({
      url: app.globalData.prefix + "/corpsAction/kickOut.do",
      data: {
        openId: app.globalData.userInfo.openId,
        memberId: memberId
      },
      method: 'POST',
      success: (res) => {
        let status = res.data.status
        if (status === "00") {
          wx.$showToast(res.data.result)
          this.onShow()
        } else {
          wx.$showToast(res.data.errorMsg)
        }
      }
    })
  },
  apply() {
    let post = this.data.post
    if(post==='03'){
      wx.$showToast('暂无权限查看申请人员')
    } else {
      wx.navigateTo({
        url: '../comeGroup/comeGroup?corpsId='+this.data.corpsId+'&name='+this.data.name
      })
    }
  },
  closeCrops() {
    wx.showModal({
      content:'确定解散兵团',
      success:(res)=>{
        if (res.confirm) {
          wx.request({
            url: app.globalData.prefix + "/corpsAction/close.do",
            data: {
              openId: app.globalData.userInfo.openId,
              corpsId: this.data.corpsId
            },
            method: 'POST',
            success: (res) => {
              let status = res.data.status
              if (status === "00") {
                wx.$showToast(res.data.result)
                wx.navigateBack()
              } else {
                wx.$showToast(res.data.errorMsg)
              }
            }
          })
        }
      }
    })
  },
  quitCrops() {
    wx.showModal({
      content: '确定退出兵团',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: app.globalData.prefix + "/corpsAction/quit.do",
            data: {
              openId: app.globalData.userInfo.openId,
              corpsId: this.data.corpsId
            },
            method: 'POST',
            success: (res) => {
              let status = res.data.status
              if (status === "00") {
                wx.$showToast(res.data.result)
                wx.navigateBack()
              } else {
                wx.$showToast(res.data.errorMsg)
              }
            }
          })
        }
      }
    })
  }
})